package com.heatup.apt.api.controller;

import java.util.HashMap;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import com.heatup.apt.api.service.ProductServiceService;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import com.heatup.apt.common.constants.ErrorCodeConstants;
import com.heatup.apt.common.util.JsonTools;
import com.heatup.apt.common.util.RequestTools;
import com.heatup.apt.common.util.ResponseTools;
import com.heatup.apt.model.api.ApiResultModel;

/**
 *
 * ProductService 控制层
 *
 */
@Controller
@RequestMapping("/product")
public class ProductServiceController extends BaseController{
	
	Logger logger = LoggerFactory.getLogger(ProductServiceController.class);
	
	@Autowired
	private ProductServiceService productServiceService;
	
	/**
	 * 查询服务列表
	 * @param request
	 * @param response
	 * @return
	 */
	@ResponseBody
	@RequestMapping(value = "/product_service_list", method = {RequestMethod.POST, RequestMethod.GET})
	public String getProductServiceList(HttpServletRequest request,HttpServletResponse response){
		ApiResultModel<Object> apiResultModel = new ApiResultModel<>();
		String callback = RequestTools.RequestString(request, "callback", "");
		String type = RequestTools.RequestString(request, "type", "2");
		Integer userId = this.getUserId();
		apiResultModel.setLogined(this.isLogined());
		Map<String,Object> parameMap = new HashMap<>();
		try {
			parameMap.put("userId", userId);
			parameMap.put("type", type);
			parameMap = productServiceService.queryProductServiceList(parameMap);
			apiResultModel.setErrorCode(apiResultModel.RET_SUCCESS);
			apiResultModel.setMsg("查询产品服务列表成功！");
			apiResultModel.setRet(apiResultModel.RET_SUCCESS);
			apiResultModel.setData(parameMap);
		} catch (Exception e) {
			apiResultModel.setMsg(ErrorCodeConstants.Code.SYSTEM_ERROR_EXCEPTION.getMessage());
			apiResultModel.setErrorCode(ErrorCodeConstants.Code.SYSTEM_ERROR_EXCEPTION.getCode());
			apiResultModel.setRet(ErrorCodeConstants.Code.SYSTEM_ERROR_EXCEPTION.getCode());
			logger.info("================== product_service_list error:"+e.getMessage());
			logger.error("==================product_service_list error:"+e.getMessage());
		}
		return ResponseTools.CallBackResultJsonP(JsonTools.jsonSer(apiResultModel), callback);
	}
	
	/**
	 * 查询产品可支持的服务门店
	 * @param request
	 * @param response
	 * @return
	 */
	@ResponseBody
	@RequestMapping(value = "/product_service_store", method = {RequestMethod.POST, RequestMethod.GET})
	public String getProductServiceStore(HttpServletRequest request,HttpServletResponse response){
		ApiResultModel<Object> apiResultModel = new ApiResultModel<>();
		String callback = RequestTools.RequestString(request, "callback", "");
		Integer id = RequestTools.RequestInt(request, "productId", 0);
		String longitudeLatitude = RequestTools.RequestString(request, "longitudeLatitude", null);
		apiResultModel.setLogined(this.isLogined());
		Map<String,Object> parameMap = new HashMap<>();
		//未登陆
        if (!isLogined()) {
            apiResultModel.setRet(ApiResultModel.RET_ERROR);
            apiResultModel.setErrorCode(ErrorCodeConstants.Code.USER_NOT_LOGIN.getCode());
            apiResultModel.setMsg(ErrorCodeConstants.Code.USER_NOT_LOGIN.getMessage());
            return ResponseTools.CallBackResultJsonP(JsonTools.jsonSer(apiResultModel), callback);
        }
		try {
			parameMap.put("id", id);
			parameMap.put("longitudeLatitude", longitudeLatitude);
			parameMap.put("userId", this.getUserId());
			parameMap = productServiceService.queryProductServiceStore(parameMap);
			apiResultModel.setErrorCode(apiResultModel.RET_SUCCESS);
			apiResultModel.setMsg("查询产品服务列表成功！");
			apiResultModel.setRet(apiResultModel.RET_SUCCESS);
			apiResultModel.setData(parameMap);
		} catch (Exception e) {
			apiResultModel.setMsg(ErrorCodeConstants.Code.SYSTEM_ERROR_EXCEPTION.getMessage());
			apiResultModel.setErrorCode(ErrorCodeConstants.Code.SYSTEM_ERROR_EXCEPTION.getCode());
			apiResultModel.setRet(ErrorCodeConstants.Code.SYSTEM_ERROR_EXCEPTION.getCode());
			logger.info("================== product_service_store error:"+e.getMessage());
			logger.error("==================product_service_store error:"+e.getMessage());
		}
		return ResponseTools.CallBackResultJsonP(JsonTools.jsonSer(apiResultModel), callback);
	}
	
	/**
	 * 查询产品详情列表
	 * @param request
	 * @param response
	 * @return
	 */
	@ResponseBody
	@RequestMapping(value = "/product_service_detail", method = {RequestMethod.POST, RequestMethod.GET})
	public String getProductServiceDetail(HttpServletRequest request,HttpServletResponse response){
		ApiResultModel<Object> apiResultModel = new ApiResultModel<>();
		String callback = RequestTools.RequestString(request, "callback", "");
		Integer id = RequestTools.RequestInt(request, "id", 0);
		apiResultModel.setLogined(this.isLogined());
		Map<String,Object> parameMap = new HashMap<>();
		if(id == 0){
			apiResultModel.setMsg(ErrorCodeConstants.Code.SYSTEM_ERROR_EXCEPTION.getMessage());
			apiResultModel.setErrorCode(ErrorCodeConstants.Code.SYSTEM_ERROR_EXCEPTION.getCode());
			apiResultModel.setRet(ErrorCodeConstants.Code.SYSTEM_ERROR_EXCEPTION.getCode());
			logger.info("================== product_service_detail error productId is null:");
			logger.error("==================product_service_detail error productId is null");
			return ResponseTools.CallBackResultJsonP(JsonTools.jsonSer(apiResultModel), callback);
		}
		try {
			parameMap.put("id", id);
			parameMap.put("userId", this.getUserId());
			parameMap = productServiceService.queryProductServiceDetail(parameMap);
			apiResultModel.setErrorCode(apiResultModel.RET_SUCCESS);
			apiResultModel.setMsg("查询产品服务列表成功！");
			apiResultModel.setRet(apiResultModel.RET_SUCCESS);
			apiResultModel.setData(parameMap);
		} catch (Exception e) {
			apiResultModel.setMsg(ErrorCodeConstants.Code.SYSTEM_ERROR_EXCEPTION.getMessage());
			apiResultModel.setErrorCode(ErrorCodeConstants.Code.SYSTEM_ERROR_EXCEPTION.getCode());
			apiResultModel.setRet(ErrorCodeConstants.Code.SYSTEM_ERROR_EXCEPTION.getCode());
			logger.info("================== product_service_list error:"+e.getMessage());
			logger.error("==================product_service_list error:"+e.getMessage());
		}
		return ResponseTools.CallBackResultJsonP(JsonTools.jsonSer(apiResultModel), callback);
	}
	

}